package com.htjx.mall.admin.controllers;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.htjx.mall.admin.controllers.vo.CategoryQuery;
import com.htjx.mall.commons.api.Result;
import com.htjx.mall.commons.models.product.Category;
import com.htjx.mall.commons.utils.page.Page;
import com.htjx.mall.services.CategoryService;

@Controller
@RequestMapping("/admin/category")
public class _CategoryController {
	@Autowired
	private CategoryService categoryService;
	
	/****
	 * 查询商品种类
	 * @param productQuery
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String productList(CategoryQuery categoryQuery, Model model) {
		Page<Category> page = new Page<Category>();
		page.setPageSize(categoryQuery.getPageSize());
		page.setPageNo(categoryQuery.getPageNo());
		page = categoryService.page(categoryQuery, page);
		
		model.addAttribute("page", page);
		return "category/list";
	}
	
	@RequestMapping(value = "/add", method=RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("category", new Category());
		return "category/add";
	}
	
	/****
	 * 新增一个种类
	 * @param productQuery
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method=RequestMethod.POST)
	public String add(Category category, Model model) {
		String name = category.getName();
		if(StringUtils.isBlank(name)) {
			model.addAttribute("result", new Result(9,"请填写种类名称"));
			return "category/add";
		}
		
		if(categoryService.checkUniqueByProperty("name", name)) {
			model.addAttribute("result", new Result(9,"该种类已被添加"));
			return "category/add";
		}
		
		String description = category.getDescription();
		if(StringUtils.isBlank(description)) {
			model.addAttribute("result", new Result(9,"请填写种类描述"));
			return "category/add";
		}
		
		category.setCreate_time(new Date());
		categoryService.insertOne(category);
		model.addAttribute("result", new Result(0,"种类添加成功"));
		return "redirect:list";
	}
	
	@RequestMapping(value = "/detail/{category_id}", method = RequestMethod.GET)
	public String categoryDetail(@PathVariable("category_id") long id, Model model) {
		Category category = categoryService.selectById(id);
		Result result = null;
		if(category == null) {
			result = new Result(9,"查询商品失败");
		} 
		
		model.addAttribute("result", result);
		model.addAttribute("category", category);
		return "category/detail";
	}
	
	@RequestMapping(value="/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") long id, Model model) {
		Category category = categoryService.selectById(id);
		model.addAttribute("category", category);
		return "category/update";
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public String update(Category category, Model model) {
		Category ref = categoryService.selectById(category.getId());
		if(ref == null) {
			Result result = new Result(9, "数据有误");
			model.addAttribute("result", result);
			return "redirect:/admin/category/detail/"+category.getId();
		}
		
		String name = category.getName();
		if(StringUtils.isBlank(name)) {
			model.addAttribute("result", new Result(9,"请填写品类名称"));
			return "redirect:/admin/category/detail/"+category.getId();
		}
		categoryService.updateOne(category);
		return "redirect:/admin/category/list";
	}
	
	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable("id") long id) {
		categoryService.deleteOne(id);
		return "redirect:/admin/category/list";
	}
	
}
